package admin

import (
	"context"
	"fmt"

	"oil-station-backend/internal/svc"
	"oil-station-backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateInventoryLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateInventoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateInventoryLogic {
	return &UpdateInventoryLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateInventoryLogic) UpdateInventory(req *types.UpdateInventoryReq) (resp *types.Inventory, err error) {
	// 验证库存数量
	if req.Stock < 0 {
		return nil, fmt.Errorf("库存数量不能为负数")
	}

	// 更新库存
	inventory, err := l.svcCtx.InventoryModel.UpdateStock(req.Id, req.Stock)
	if err != nil {
		return nil, err
	}

	// 转换为API响应格式
	return &types.Inventory{
		Id:       inventory.Id,
		FuelType: inventory.FuelType,
		Stock:    inventory.Stock,
		MinStock: inventory.MinStock,
		MaxStock: inventory.MaxStock,
	}, nil
}